package com.qfedu.service.impl;

import com.qfedu.common.JsonResult;
import com.qfedu.dao.UserDao;
import com.qfedu.entity.User;
import com.qfedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * 登陆账户页面
 * projectName: bankSystem
 * author: CGY
 * time: 2020/9/3 19:59
 * description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public User login(String bankCode, String password) {
        User user = userDao.findByCode(bankCode);

        if (user == null) {
            throw new RuntimeException("账户错误");
        }
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }
        return user;
    }

    @Override
    public void updateHeadImg(Integer id, String imgPath) {
        User u = new User();
        u.setId(id);
        u.setImgPath(imgPath);
        userDao.update(u);
    }



    @Override
    public User selectByCode(String bankCode) {
        return userDao.findByCode(bankCode);
    }

    /**
     * 修改密码
     * @param bankCode
     * @param oldPassword
     * @param newPassword
     */
    @Override
    public void updatePassword(String bankCode, String oldPassword, String newPassword) {

        //获取用户信息
        User user = userDao.findByCode(bankCode);

        //原密码比较
        if (!user.getPassword().equals(oldPassword)){
            System.out.println(oldPassword);
            throw new RuntimeException("原密码错误，请检查");
        }

        User user1 = new User();
        user1.setId(user.getId());
        user1.setPassword(newPassword);
        userDao.update(user1);

    }

    @Override
    public JsonResult insertUser(User user) {

        User user1= userDao.findByCode(user.getBankCode());
        if (user1 != null){
            JsonResult jsonResult = new JsonResult(0, "注册失败，该账户已存在");
            return jsonResult;
        }

        userDao.insertUser(user);
        JsonResult jsonResult = new JsonResult(1, "注册成功");
        return jsonResult;
    }
}